/* $RCSFile: ADNDynIncident.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynIncident.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynIncident : DynEntity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.incident)
            {}

            public override Type DynType
            { get { return typeof(ADNDynIncident); } }

            public override string DynTypename
            {   get{ return CrmStr.incident;}}

            public override DynEntity Build()
            {   return new ADNDynIncident();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynIncident(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynIncident(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynIncident(w,PrimaryKeyValue);}
        }

        static ADNDynIncident(){new Ftor();} 

        public override bool HasOwningBusinessUnitProperty
        {
            get{   return true;}
        }

		/// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynIncident(): base()
		{}

        public ADNDynIncident(CRMWrapperBase w, string PrimaryFiedlValue)
			: base(w,CrmStr.incident)
		{
			title = PrimaryFiedlValue;
		}

		public ADNDynIncident(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.incident, CrmId)
		{}

		public ADNDynIncident(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.incident,e,null,null)
		{}

		public ADNDynIncident(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(w,CrmStr.incident,e,prop,s)
		{}

        public ADNDynIncident(ResetWrapperHandler fn, string PrimaryFiedlValue)
			: base(fn,CrmStr.incident)
		{
			title = PrimaryFiedlValue;
		}

		public ADNDynIncident(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.incident, CrmId)
		{}

		public ADNDynIncident(ResetWrapperHandler fn,InternalDynamicEntity e)
			: base(fn,CrmStr.incident,e,null,null)
		{}

		public ADNDynIncident(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(fn,CrmStr.incident,e,prop,s)
		{}

        [CrmDynAttribute(CrmStr.incident)]
        public Guid incidentid
		{
			get
			{
				return KeyPropertyValue(CrmStr.incidentid);
			}
			set
			{
				AddKeyProperty(CrmStr.incidentid, value);
			}
		}
        
        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.incidentid) || KeyPropertyValue(CrmStr.incidentid) != crmId )
                incidentid  =   crmId;
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return incidentid;
           }
           set
           {
               incidentid    =   value;
           }
        }

        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.title;
            }
           set
           {}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return title;
            }
            set
            {
                title =   value;
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public string description
        {
            get
            {
                return StringPropertyValue(CrmStr.description);
            }
            set
            {
                AddStringProperty(CrmStr.description,value);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public string title 
        {
            get 
            {
                return StringPropertyValue(CrmStr.title);
            }
            set 
            {
                AddStringProperty(CrmStr.title,value);
            }
        }
        
        #region customer

        string _DefaultCustomerName   =   CrmStr.account;
		public string DefaultCustomerName
		{
			get
			{
				return _DefaultCustomerName;
			}
			set
			{
                switch (value)
                {
                    case CrmStr.account:
                    case CrmStr.contact:
                        _DefaultCustomerName  =   value;
                        break;
                }
			}
		}

        /// <summary>
        /// Beware : set defaults to account, to change it set DefaultCustomerName property before calling set
        /// or use SetContactCustomerid for adding contacts
        /// </summary>
        [CrmDynAttribute(CrmStr.incident)]
        public Guid customerid 
        {
            get 
            {
                return CustomerPropertyValue(CrmStr.customerid);
            }
            set 
            {
                AddCustomerProperty(CrmStr.customerid,DefaultCustomerName,value);
            }
        }

        /// <summary>
        /// The entity : account or contact
        /// </summary>
        public string customerType
        {
            get 
            {
                return CustomerPropertyType(CrmStr.customerid);
            }
        }

         /// <summary>
        /// To adding contact customerids
        /// </summary>
        public void SetContactCustomerid(Guid contactid)
        {
            AddCustomerProperty(CrmStr.customerid,CrmStr.contact,contactid);
        }

        #endregion customer
        [CrmDynAttribute(CrmStr.incident)]
        public Guid subjectid 
        {
			get
			{
				return LookupPropertyValue(CrmStr.subjectid);
			}
			set
			{
				AddLookupProperty(CrmStr.subjectid,CrmStr.subject, value);
			}
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public string subjectName 
        {
			get
			{
                if ( ContainsKey(CrmStr.subjectid) )
				    return LookupPropertyName(CrmStr.subjectid);
                return string.Empty;
			}
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public string ticketnumber 
        {
            get 
            {
                return StringPropertyValue(CrmStr.ticketnumber);
            }
            set 
            {
                AddStringProperty(CrmStr.ticketnumber,value);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public Guid productid 
        {
			get
			{
				return LookupPropertyValue(CrmStr.productid);
			}
			set
			{
				AddLookupProperty(CrmStr.productid,CrmStr.product, value);
			}
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public string productserialnumber 
        {
            get 
            {
                return StringPropertyValue(CrmStr.productserialnumber);
            }
            set 
            {
                AddStringProperty(CrmStr.productserialnumber,value);
            }
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public Guid responsiblecontactid 
        {
			get
			{
				return LookupPropertyValue(CrmStr.responsiblecontactid);
			}
			set
			{
				AddLookupProperty(CrmStr.responsiblecontactid,CrmStr.contact, value);
			}
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public Guid contractdetailid 
        {
			get
			{
				return LookupPropertyValue(CrmStr.contractdetailid);
			}
			set
			{
				AddLookupProperty(CrmStr.contractdetailid,CrmStr.contractdetail, value);
			}
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public Guid contractid 
        {
			get
			{
				return LookupPropertyValue(CrmStr.contractid);
			}
			set
			{
				AddLookupProperty(CrmStr.contractid,CrmStr.contract, value);
			}
        }

        [CrmDynAttribute(CrmStr.incident)]
        public int actualserviceunits 
        {
            get 
            {
                return NumberPropertyValue(CrmStr.actualserviceunits);
            }
            set 
            {
                AddNumberProperty(CrmStr.actualserviceunits,value);
            }
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public int billedserviceunits 
        {
            get 
            {
                return NumberPropertyValue(CrmStr.billedserviceunits);
            }
            set 
            {
                AddNumberProperty(CrmStr.billedserviceunits,value);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public bool isdecrementing 
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.isdecrementing);
            }
            set 
            {
                AddBooleanProperty(CrmStr.isdecrementing,value);
            }
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public Guid kbarticleid 
        {
			get
			{
				return LookupPropertyValue(CrmStr.kbarticleid);
			}
			set
			{
				AddLookupProperty(CrmStr.kbarticleid,CrmStr.kbarticle, value);
			}
        }

        [CrmDynAttribute(CrmStr.incident)]
        public int severitycode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.severitycode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.severitycode,value);
            }
        }

        public string severitycodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.severitycode);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public int contractservicelevelcode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.contractservicelevelcode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.contractservicelevelcode,value);
            }
        }
        
        public string contractservicelevelcodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.contractservicelevelcode);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public int customersatisfactioncode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.customersatisfactioncode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.customersatisfactioncode,value);
            }
        }

        public string customersatisfactioncodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.customersatisfactioncode);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public int incidentstagecode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.incidentstagecode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.incidentstagecode,value);
            }
        }
        
        public string incidentstagecodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.incidentstagecode);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public int prioritycode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.prioritycode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.prioritycode,value);
            }
        }

        public string prioritycodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.prioritycode);
            }
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public int caseorigincode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.caseorigincode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.caseorigincode,value);
            }
        }
        
        public string caseorigincodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.caseorigincode);
            }
        }

        [CrmDynAttribute(CrmStr.incident)]
        public int casetypecode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.casetypecode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.casetypecode,value);
            }
        }

        public string casetypecodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.casetypecode);
            }
        }
        
        [CrmDynAttribute(CrmStr.incident)]
        public string followupby 
        {
            get 
            {
                return DateTimePropertyValue(CrmStr.followupby);
            }
            set 
            {
                AddDateTimeProperty(CrmStr.followupby,value);
            }
        }

        /// <summary>
        /// virtual because on 'detail' records it is only a uniqueidentifier property
        /// </summary>
        [CrmDynAttribute(CrmStr.incident)]
        public Guid owningbusinessunit
        {
            get
            {
                return LookupPropertyValue(CrmStr.owningbusinessunit);
            }
            set
            {
                AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value);
            }
        }
        
        public string owningbusinessunitName
        {
            get
            {
                return LookupPropertyName(CrmStr.owningbusinessunit);
            }
        }

        /// <summary>
        /// Owner for this object
        /// </summary>
        [CrmDynAttribute(CrmStr.incident)]
        public override Guid ownerid
        {
            get
            {
                if ( ContainsKey(CrmStr.ownerid) )
                    return OwnerPropertyValue(CrmStr.ownerid);
                return Guid.Empty;
            }
            set
            {
                AddOwnerProperty(CrmStr.ownerid,CrmStr.systemuser,value);
            }
        }

        #region utilities

        public void Route(ADNDynQueue dynQ,RouteType type,bool isAdmin)
        {
            wrapper.Util.RouteIncident(this,dynQ,type,isAdmin);
        }

        public ADNDynCustomer RetrieveCustomer()
        {
            if (CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynIncident RetrieveCustomer BEG Entity: {0}, customerid: {1}",
                    customerType,customerid),CRMDynTraceSwitch.Sw.Info);
            
            switch(customerType)
            {
                case CrmStr.account:
                    ADNDynAccount waccount  =   new ADNDynAccount(wrapper,customerid);
                    waccount.adminRetrieve();
                    if ( waccount != null )
                    {
                        if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynIncident RetrieveCustomer found account: {0}-{1}",
                                waccount.name,waccount.accountid),CRMDynTraceSwitch.Sw.Info);
                        return waccount;
                    }
                    break;
                case CrmStr.contact:
                    ADNDynContact wcontact   =   new ADNDynContact(wrapper,customerid);
                    wcontact.adminRetrieve();
                    if ( wcontact != null )
                    {
                        if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynIncident RetrieveCustomer found contact: {0}-{1}",
                                wcontact.lastname,wcontact.contactid),CRMDynTraceSwitch.Sw.Info);
                        return wcontact;
                    }
                    break;

                case CrmStr.lead:
                    ADNDynLead wlead         =   new ADNDynLead(wrapper,customerid);
                    wlead.adminRetrieve();
                    if ( wlead != null )
                    {
                        if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynIncident RetrieveCustomer found lead: {0}-{1}",
                                wlead.lastname,wlead.leadid),CRMDynTraceSwitch.Sw.Info);
                        return wlead;
                    }
                    break;
            }
            return null;
        }

        #endregion utilities
	}
}
